# -*- coding: utf-8 -*-
# Create time: 2020-03-10 17:21
# Author:Rongzhikai
from conf.settings import JSON_FILE
import json
import ast


class Datafromjson:
    """参数从配置好的json文件读取，便于Excel文件编辑和查看"""

    def __init__(self, datajson=JSON_FILE):
        with open(datajson, encoding='utf-8') as fp:
            self.data = json.load(fp)

    def getdata(self, key):
        if key is None:
            return None
        elif key.strip().startswith('{'):  # 如果参数本身就是一个字典，无需从json数据文件查找
            return ast.literal_eval(key)
        else:
            return self.data.get(key, None)


if __name__ == '__main__':
    a = Datafromjson().getdata('{"a":3232,"b":"pop"}')
    print(a)
    print(type(a))
